package com.imooc.item.controller;

import com.imooc.controller.BaseController;
import com.imooc.item.pojo.Items;
import com.imooc.item.pojo.ItemsImg;
import com.imooc.item.pojo.ItemsParam;
import com.imooc.item.pojo.ItemsSpec;
import com.imooc.item.pojo.vo.CommentLevelCountsVO;
import com.imooc.item.pojo.vo.ShopcartVO;
import com.imooc.item.service.ItemService;
import com.imooc.pojo.IMOOCJSONResult;
import com.imooc.pojo.PagedGridResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: 顾志杰
 * @Date: 2019/11/13
 * @Time: 9:33
 */


@RestController
@RequestMapping("items")
@Api(value = "商品接口", tags = "商品接口")
public class itemsController extends BaseController{

    @Autowired
    private ItemService itemService;


    @ApiOperation(value = "查询商品详情", notes = "查询商品详情", httpMethod = "GET")
    @GetMapping("/info/{itemId}")
    public IMOOCJSONResult info(@ApiParam(name = "itemId", value = "商品id", required = true)
                                @PathVariable String itemId) {
        if (StringUtils.isBlank(itemId)) {
            return IMOOCJSONResult.errorMsg(null);
        }
        Items items = itemService.queryItemById(itemId);
        List<ItemsImg> itemsImgList = itemService.queryItemImgList(itemId);
        ItemsParam itemsParam = itemService.queryItemParam(itemId);
        List<ItemsSpec> itemsSpecList = itemService.queryItemSpecList(itemId);
        Map<String, Object> map = new HashMap<>();
        map.put("item", items);
        map.put("itemImgList", itemsImgList);
        map.put("itemParams", itemsParam);
        map.put("itemSpecList", itemsSpecList);
        return IMOOCJSONResult.ok(map);
    }


    @ApiOperation(value = "查询商品评价等级数量", notes = "查询商品评价等级数量", httpMethod = "GET")
    @GetMapping("/commentLevel")
    public IMOOCJSONResult commentLevel(@ApiParam(name = "itemId", value = "商品id", required = true)
                                        @RequestParam String itemId) {
        if (StringUtils.isBlank(itemId)) {
            return IMOOCJSONResult.errorMsg(null);
        }
        CommentLevelCountsVO commentLevelCountsVO = itemService.queryCommentCounts(itemId);
        return IMOOCJSONResult.ok(commentLevelCountsVO);
    }


    @ApiOperation(value = "查询商品评论", notes = "查询商品评论", httpMethod = "GET")
    @GetMapping("/comments")
    public IMOOCJSONResult comments(@ApiParam(name = "itemId", value = "商品id", required = true)
                                    @RequestParam String itemId,
                                    @ApiParam(name = "level", value = "评价等级", required = false)
                                    @RequestParam Integer level,
                                    @ApiParam(name = "page", value = "页码", required = false)
                                    @RequestParam Integer page,
                                    @ApiParam(name = "pageSize", value = "页容", required = false)
                                    @RequestParam Integer pageSize) {
        if (StringUtils.isBlank(itemId)) {
            return IMOOCJSONResult.errorMsg(null);
        }
        if(page==null) {
            page=PAGE;
        }
        if(pageSize==null) {
            pageSize=COMMON_PAGE_SIZE;
        }
        PagedGridResult comments = itemService.queryPagedComments(itemId, level, page, pageSize);
        return IMOOCJSONResult.ok(comments);
    }




    @ApiOperation(value = "根据商品规格ids查询最新的商品数据", notes = "根据商品规格ids查询最新的商品数据", httpMethod = "GET")
    @GetMapping("/refresh")
    public IMOOCJSONResult refresh(@ApiParam(name = "itemSpecIds", value = "拼接的规格id", required = true,example = "1001,1003,1005")
                                    @RequestParam String itemSpecIds
                                    ) {
        if (StringUtils.isBlank(itemSpecIds)) {
            return IMOOCJSONResult.errorMsg(null);
        }
        List<ShopcartVO> shopcartVOS = itemService.queryItemsBySpecIds(itemSpecIds);
        return IMOOCJSONResult.ok(shopcartVOS);
    }
}
